<?php


namespace App\Tool;


class Category
{

    /**
     * Create by Peter
     * 2019/11/12 16:27:29
     * Email:904801074@qq.com
     * @param $pid
     * @return array
     */
    static function getCategoryByPid($pid,$isCache=false,$expiredTime=60){

        $key='all_category_list';

        if($isCache){

            $list=\Cache::get($key,null);

            if($list) return json_decode($list,true);

            $list=(new self())->c($pid);

            \Cache::put($key,json_encode($list),$expiredTime);

            return  $list;

        }else{

            return (new self())->c($pid);
        }


    }

    /**
     * Create by Peter
     * 2019/11/12 16:27:39
     * Email:904801074@qq.com
     * @param $pid
     * @return array
     */
    private function c($pid){

        static $all=[];

        $list= \App\Model\Category::where('pid',$pid)->get();

        if($list) {

            foreach ($list as $key => $value) {

                $all[] = $value->toArray();

                $id = $value->id;

                $this->c($id);


            }

        }

        return array_values($all);

    }

}
